package org.androidpn.fragment;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.R;
import org.androidpn.activity.MainActivity;
import org.androidpn.activity.TalkingActivity;
import org.androidpn.adapter.TalkListAdapter;
import org.androidpn.fragment.Fragment2.DeleteUserinfoListener;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CTalkList;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment1 extends Fragment implements OnItemClickListener, OnItemLongClickListener {
	private ListView msgHistoryLv;
	TalkListAdapter adapter;
	private List<CTalkList> talkLists = new ArrayList<CTalkList>();

	public Fragment1() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View contentView = inflater.inflate(R.layout.fragment_fragment1, container, false);
		msgHistoryLv = (ListView) contentView.findViewById(R.id.lv_msg_history);
		msgHistoryLv.setOnItemClickListener(this);
		adapter = new TalkListAdapter(getActivity());
		adapter.setcTalkLists(talkLists);
		msgHistoryLv.setAdapter(adapter);
		msgHistoryLv.setOnItemClickListener(this);
		msgHistoryLv.setOnItemLongClickListener(this);
		GetTalkListResponseListener listener = new GetTalkListResponseListener();
		HttpApi.getTalkList(((MainActivity) getActivity()).cUserInfo.getTelephone(), listener, listener);
		return contentView;
	}

	@Override
	public void onResume() {
		GetTalkListResponseListener listener = new GetTalkListResponseListener();
		HttpApi.getTalkList(((MainActivity) getActivity()).cUserInfo.getTelephone(), listener, listener);
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(getActivity(), TalkingActivity.class);
		intent.putExtra("ctalklist", talkLists.get(arg2));
		startActivity(intent);
	}

	
	class GetTalkListResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(getActivity(), "网络异常");
		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				talkLists = JsonUtil.parseArray(netModel.getContent(), CTalkList.class);
				adapter.setcTalkLists(talkLists);
				adapter.notifyDataSetChanged();
			}
			// 根据a-z进行排序源数据
			// filledData(sourceDateList);
			// Collections.sort(sourceDateList, pinyinComparator);
			// adapter.updateListView(sourceDateList);
		}

	}


	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int arg2,
			long arg3) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		 AlertDialog.Builder builder = new AlertDialog.Builder(Fragment1.this.getActivity());  
	        builder.setTitle("删除消息？");  
	        final CTalkList cTalkList = ((CTalkList) adapter.getItem(arg2));
	        builder.setPositiveButton("确定",  
	                new DialogInterface.OnClickListener() {  
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    	DeleteTalkListListener deleteUserinfoListener =new DeleteTalkListListener(arg2);
	                    	HttpApi.deleteTalkList(cTalkList.getFriend_telephone(), cTalkList.getMy_telephone(), deleteUserinfoListener, deleteUserinfoListener);
	                    }  
	                });  
	        builder.setNegativeButton("取消",  
	                new DialogInterface.OnClickListener() {  
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    	
	                    }  
	                });  
	        builder.show();  
		return true;
	}

	class DeleteTalkListListener implements Listener<String>,ErrorListener{
        int position;
        public DeleteTalkListListener(int position){
        	this.position = position;
        }
		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			NetModel netModel =JsonUtil.parseObject(response, NetModel.class);
			if(netModel.getCode() ==ConstanceUtil.REQUEST_SUCCESS){
				ToastUtil.showShortToast(Fragment1.this.getActivity(), netModel.getContent());
				talkLists.remove(position);
				adapter.notifyDataSetChanged();
			}
		}
		
	}
}
